package net.uhuijia.zb.userzhubolivefeedback.controller;

import net.uhuijia.enums.OnliceLiveFeedBackAreaEnum;
import net.uhuijia.enums.RedisKey;
import net.uhuijia.user.usermall.persistence.model.UserMall;
import net.uhuijia.vo.ResultMapVo;
import net.uhuijia.vo.ReturnListVo;
import net.uhuijia.vo.zb.OnlineFeedBackVo;
import net.uhuijia.zb.userzhubolivefeedback.persistence.model.UserZhuboLiveFeedBack;
import net.uhuijia.zb.userzhubolivefeedback.persistence.model.UserZhuboLiveFeedBackExample;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.EAN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import net.uhuijia.appbase.controller.BaseController;
import net.uhuijia.zb.userzhubolivefeedback.service.UserZhuboLiveFeedBackService;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping(value="/userzhubolivefeedback")
public class UserZhuboLiveFeedBackController
     extends BaseController<UserZhuboLiveFeedBackService,UserZhuboLiveFeedBack,UserZhuboLiveFeedBackExample> {
    @Autowired
    private UserZhuboLiveFeedBackService userZhuboLiveFeedBackService;

    /**
     * 添加直播评论
     * @param liveId
     * @param content
     * @return
     */
    @RequestMapping(value = "addUserZhuboLiveFeedBack",method = RequestMethod.GET)
    @ResponseBody
    public ResultMapVo addUserZhuboLiveFeedBack(String liveId,String content){
        if(StringUtils.isNoneBlank(liveId,content)){
            UserMall userMall = this.getUserMallByTokenAndDevice();
            UserZhuboLiveFeedBack userZhuboLiveFeedBack = this.userZhuboLiveFeedBackService.addUserZhuboLiveFeedBack(liveId,userMall.getId(),content);
            if(userZhuboLiveFeedBack!=null){
                //将评理内容放入到队列里
                StringBuffer onlineLiveFeedBackKey = new StringBuffer(RedisKey.LIVE_ONLINE_USER_FEED_BACK.getKey());
                onlineLiveFeedBackKey.append(liveId);
                OnlineFeedBackVo onlineFeedBackVo = new OnlineFeedBackVo(userMall.getNickName(), OnliceLiveFeedBackAreaEnum.FeedBack.getType(),content);
                this.redisDao.leftPush(onlineLiveFeedBackKey.toString(),onlineFeedBackVo);
            }
        }else
            throw  new IllegalArgumentException();
        return ResultMapVo.ok();
    }

    /**
     * 直播评论内容
     * @param liveId 直播Id
     * @return
     */
    @RequestMapping(value = "contentList",method = RequestMethod.GET)
    @ResponseBody
    public ResultMapVo<List<OnlineFeedBackVo>> contentList(String liveId){
        if(StringUtils.isEmpty(liveId))
            throw  new IllegalArgumentException();
        StringBuffer onlineFeedBackKey = new StringBuffer(RedisKey.LIVE_ONLINE_USER_FEED_BACK.getKey());
        onlineFeedBackKey.append(liveId);
        List<OnlineFeedBackVo> onlineFeedBackVos = new ArrayList<>();
        int length = 10;
        while (length>0){
            OnlineFeedBackVo onlineFeedBackVo = (OnlineFeedBackVo)this.redisDao.rightPop(onlineFeedBackKey.toString());
            if(onlineFeedBackVo==null){
                break;
            }else{
                onlineFeedBackVos.add(onlineFeedBackVo);
            }
            length--;
        }
        return ResultMapVo.ok(onlineFeedBackVos);
    }


}